<?php
#####################################
# * @Author: lehieu008              #
# * @Email: lehieu008@gmail.com     #
# * @Copyright: 2008 - 2010         #
#####################################
if(!defined('BASEPATH'))exit('No direct script access allowed');
class Bar_module extends Controller
{
    function __construct()
    {
        parent::Controller();
    }
    
    function index($buttons = array(), $total = 0, $rows = 10)
    {
        if(is_int($total) && $total > 0)
        {
            $url = base_url().$this->uri->segment(1).'/';
            #BEGIN: getVar
            switch(trim(strtoupper($this->uri->segment(1))))
            {
                case 'MEMBER':
                    $segment = 3;
                    $url .= $this->uri->segment(2).'/';
                    break;
                case 'ACCOUNT':
                    switch(trim(strtoupper($this->uri->segment(3))))
                    {
                        case 'FAVORITE':
                        case 'RECEIVED':
                        case 'SEND':
                        case 'INVITE':
                            $segment = 4;
                            $url .= $this->uri->segment(2).'/'.$this->uri->segment(3).'/';
                            break;
                        default:
                            $segment = 3;
                            $url .= $this->uri->segment(2).'/';
                    }
                    break;
                default:
                    switch(trim(strtoupper($this->uri->segment(2))))
                    {
                        case 'SEARCH':
                            $segment = 3;
                            $url .= $this->uri->segment(2).'/';
                            break;
                        default:
                            $segment = 2;
                    }
            }
            $segments = array('section', 'category', 'name', 'title', 'hot', 'bestShop', 'vip', 'reliable', 'saleOff', 'fromPrice', 'toPrice', 'fromSalary', 'toSalary', 'currency', 'unitSalary', 'fromView', 'toView', 'address', 'province', 'fromDate', 'toDate', 'search', 'keyword', 'sort', 'by', 'page');
            $getVar = $this->uri->uri_to_assoc($segment, $segments);
            #END getVar
            #BEGIN: Section
            if(isset($getVar['section']) && $getVar['section'] !== FALSE && (int)$getVar['section'] > 0)
            {
                $url .= 'section/'.$getVar['section'].'/';
            }
            #END Section
            #BEGIN: Category
            if(isset($getVar['category']) && $getVar['category'] !== FALSE && (int)$getVar['category'] > 0)
            {
                $url .= 'category/'.$getVar['category'].'/';
            }
            #END Category
            #BEGIN: Name || Title
            if(isset($getVar['name']) && $getVar['name'] !== FALSE && trim($getVar['name']) != '')
            {
                $url .= 'name/'.$getVar['name'].'/';
            }
            elseif(isset($getVar['title']) && $getVar['title'] !== FALSE && trim($getVar['title']) != '')
            {
                $url .= 'title/'.$getVar['title'].'/';
            }
            #END Name || Title
            #BEGIN: Hot
            if(isset($getVar['hot']) && $getVar['hot'] !== FALSE && (int)$getVar['hot'] == 1)
            {
                $url .= 'hot/'.$getVar['hot'].'/';
            }
            #END Hot
            #BEGIN: Best Shop
            if(isset($getVar['bestShop']) && $getVar['bestShop'] !== FALSE && (int)$getVar['bestShop'] == 1)
            {
                $url .= 'bestShop/'.$getVar['bestShop'].'/';
            }
            #END Best Shop
            #BEGIN: Vip
            if(isset($getVar['vip']) && $getVar['vip'] !== FALSE && (int)$getVar['vip'] == 1)
            {
                $url .= 'vip/'.$getVar['vip'].'/';
            }
            #END Vip
            #BEGIN: Reliable
            if(isset($getVar['reliable']) && $getVar['reliable'] !== FALSE && (int)$getVar['reliable'] == 1)
            {
                $url .= 'reliable/'.$getVar['reliable'].'/';
            }
            #END Reliable
            #BEGIN: SaleOff
            if(isset($getVar['saleOff']) && $getVar['saleOff'] !== FALSE && (int)$getVar['saleOff'] == 1)
            {
                $url .= 'saleOff/'.$getVar['saleOff'].'/';
            }
            #END SaleOff
            #BEGIN: From Price
            if(isset($getVar['fromPrice']) && $getVar['fromPrice'] !== FALSE && trim($getVar['fromPrice']) != '')
            {
                $url .= 'fromPrice/'.$getVar['fromPrice'].'/';
            }
            #END From Price
            #BEGIN: To Price
            if(isset($getVar['toPrice']) && $getVar['toPrice'] !== FALSE && trim($getVar['toPrice']) != '')
            {
                $url .= 'toPrice/'.$getVar['toPrice'].'/';
            }
            #END To Price
            #BEGIN: From Salary
            if(isset($getVar['fromSalary']) && $getVar['fromSalary'] !== FALSE && trim($getVar['fromSalary']) != '')
            {
                $url .= 'fromSalary/'.$getVar['fromSalary'].'/';
            }
            #END From Salary
            #BEGIN: To Salary
            if(isset($getVar['toSalary']) && $getVar['toSalary'] !== FALSE && trim($getVar['toSalary']) != '')
            {
                $url .= 'toSalary/'.$getVar['toSalary'].'/';
            }
            #END To Salary
            #BEGIN: Currency
            if(isset($getVar['currency']) && $getVar['currency'] !== FALSE && (trim(strtoupper($getVar['currency'])) == 'VND' || trim(strtoupper($getVar['currency'])) == 'USD'))
            {
                $url .= 'currency/'.$getVar['currency'].'/';
            }
            #END Currency
            #BEGIN: Unit Salary
            if(isset($getVar['unitSalary']) && $getVar['unitSalary'] !== FALSE && ((int)$getVar['unitSalary'] == 1 || (int)$getVar['unitSalary'] == 2 || (int)$getVar['unitSalary'] == 3))
            {
                $url .= 'unitSalary/'.$getVar['unitSalary'].'/';
            }
            #END Unit Salary
            #BEGIN: From View
            if(isset($getVar['fromView']) && $getVar['fromView'] !== FALSE && (int)$getVar['fromView'] >= 0)
            {
                $url .= 'fromView/'.$getVar['fromView'].'/';
            }
            #END From View
            #BEGIN: To View
            if(isset($getVar['toView']) && $getVar['toView'] !== FALSE && (int)$getVar['toView'] >= 0)
            {
                $url .= 'toView/'.$getVar['toView'].'/';
            }
            #END To View
            #BEGIN: Address
            if(isset($getVar['address']) && $getVar['address'] !== FALSE && trim($getVar['address']) != '')
            {
                $url .= 'address/'.$getVar['address'].'/';
            }
            #END Address
            #BEGIN: Province
            if(isset($getVar['province']) && $getVar['province'] !== FALSE && (int)$getVar['province'] >= 0)
            {
                $url .= 'province/'.$getVar['province'].'/';
            }
            #END Province
            #BEGIN: From Date
            if(isset($getVar['fromDate']) && $getVar['fromDate'] !== FALSE && trim($getVar['fromDate']) != '')
            {
                $url .= 'fromDate/'.$getVar['fromDate'].'/';
            }
            #END From Date
            #BEGIN: To Date
            if(isset($getVar['toDate']) && $getVar['toDate'] !== FALSE && trim($getVar['toDate']) != '')
            {
                $url .= 'toDate/'.$getVar['toDate'].'/';
            }
            #END To Date
            #BEGIN: Search
            if(isset($getVar['search']) && $getVar['search'] !== FALSE && trim($getVar['search']) != '' && isset($getVar['keyword']) && $getVar['keyword'] !== FALSE && trim($getVar['keyword']) != '')
            {
                $url .= 'search/'.$getVar['search'].'/keyword/'.$getVar['keyword'].'/';
            }
            #END Search
            #BEGIN: Sort
            if(isset($getVar['sort']) && $getVar['sort'] !== FALSE && trim($getVar['sort']) != '' && isset($getVar['by']) && $getVar['by'] !== FALSE && (trim(strtoupper($getVar['by'])) == 'ASC' || trim(strtoupper($getVar['by'])) == 'DESC'))
            {
                $url .= 'sort/'.$getVar['sort'].'/by/'.$getVar['by'].'/';
            }
            #END Sort
            #BEGIN: Page
            if(isset($getVar['page']) && $getVar['page'] !== FALSE && (int)$getVar['page'] > 0)
            {
                $page = (int)$getVar['page'];
            }
            else
            {
                $page = 0;
            }
            #END Page
            #BEGIN: Pagination
            $this->load->library('pagination');
            $config['base_url'] = $url.'page/';
            $config['uri_segment'] = $segment;
            $config['total_rows'] = $total;
            $config['per_page'] = $rows;
            $config['num_links'] = 1;
            $config['cur_page'] = $page;
            $this->pagination->initialize($config);
            $data['link'] = $this->pagination->create_links();
            #END Pagination
        }
        elseif(is_array($total) && count($total) == 2)
        {
            $data['total'] = $total;
        }
        $data['buttons'] = $buttons;
        #Load View
        $this->load->view('bar/defaults', $data);
    }
}